package com.experiment.aicommunityback.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.experiment.aicommunityback.entity.*;
import com.experiment.aicommunityback.mapper.RoleMapper;
import com.experiment.aicommunityback.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.experiment.aicommunityback.entity.ResultCodeEnum.*;

/**
 * @Author: 杨婉汐
 * @name: RoleServiceimpl
 * @Date: 2024/12/21  17:33
 */
@Service
public class RoleServiceimpl implements RoleService {
    private RoleMapper roleMapper;

    @Autowired
    public RoleServiceimpl(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    /**
     * 展示权限
     */
    @Override
    public List<Role> allRole() {
        return roleMapper.allRole();
    }

    /**
     * 查看各个权限的使用用户数
     */
    @Override
    public List<HashMap<String, Integer>> roleNum() {
        List<RNutil> roleNumber = roleMapper.roleNum();
        List<HashMap<String, Integer>> rnList = new ArrayList<>();
        for (RNutil i : roleNumber) {
            HashMap<String, Integer> map = new HashMap<>();
            String name = i.getName();
            long number = i.getNumber() == null ? 0 : i.getNumber();
            map.put(name, Math.toIntExact(number));
            rnList.add(map);
        }
        return rnList;

    }

    /**
     * 增加权限
     */
    @Override
    public Result addRole(Role role) {
        Role haveRole = this.queryRole(role.getName());
        if (haveRole!=null){
            return Result.setResult(ResultCodeEnum.ADD_ROLE_ERROR).data("错误原因","添加权限重复");
        }else {
            if (role.getRemark()==null) {
                role.setRemark("暂无介绍");
            }
            System.out.println("增加权限");
            return roleMapper.addRole(role) ? Result.ok() : Result.setResult(ResultCodeEnum.ADD_ROLE_ERROR);
        }
    }

    /**
     * 根据权限Id删除权限
     * @return
     */
    @Override
    public Result deleteRole(int id) {
        if (id==1 || id==2 || id==404){
            return Result.setResult(DELETE_ROLE_ERROR).data("错误原因","禁止删除1,2,404这个三个权限!");
        }
        return roleMapper.deleteRole(id)?Result.ok().data("删除权限ID",id):Result.setResult(DELETE_ROLE_ERROR);
    }

    //TODO 1root,2普通用户,404游客  这三个禁止修改

    /**
     * 修改权限
     */
    @Override
    public Result updatedRole(Role role) {
        Integer id = role.getId();
        if (id==1 || id==2 || id==404){
            return Result.setResult(UPDATED_ROLE_ERROR).data("错误原因","禁止更改1,2,404这个三个权限!");
        }
        Role haveRole = this.queryRole(role.getName());
        if (haveRole!=null){
            return Result.setResult(ResultCodeEnum.ADD_ROLE_ERROR).data("错误原因","添加权限重复");
        }else {
            return roleMapper.updatedRole(role) ? Result.ok().data("updatedRole", true) : Result.setResult(UPDATED_ROLE_ERROR);
        }
    }

    /**
     * 权限查询
     */
    @Override
    public Role queryRole(String index) {
        // 使用正则表达式判断是id(全数字)还是name，对应不同查询
        if (index.matches("\\d+")) {
            int roleId = Integer.parseInt(index);
            System.out.println("使用ID查询权限");
            return roleMapper.queryById(roleId);
        } else {
            System.out.println("使用Name查询权限");
            return roleMapper.queryByName(index);
        }
    }

    /**
     * 返回可用权限供用户修改
     */
    @Override
    public Result updatedRoleStatus(Map<String, Object> paramMap) {
        Object status = paramMap.get("status");
        Object id = paramMap.get("id");
        LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Role::getId, id).set(Role::getStatus, status);
        roleMapper.update(null, updateWrapper);
        return null;
    }

    /**
     * 返回可用权限供用户修改
     * @return
     */
    @Override
    public Result optionalRole() {
        if (roleMapper.optionalRole().isEmpty()) {
            return Result.setResult(UNKNOWN_ROLE_ERROR);
        }
        return Result.ok().data("optionalRole",roleMapper.optionalRole());
    }
}
